home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / wpj1_8.zip / SHAREMEM.ZIP / _MALLOC.C next >
C/C++ Source or Header  |  1993-07-22  |  3KB  |  110 lines

  1. /*
  2.  
  3. _MALLOC.C - Windows global memory allocation program
  4.  
  5. */
  6.  
  7.  
  8. #pragma hdrfile _malloc.sym
  9. #define STRICT
  10. #include <windows.h>
  11. #include <winstyle.h>
  12. #include <dos.h>
  13. #include <stdio.h>
  14. #pragma hdrstop
  15.  
  16.  
  17. #include <wmalloc.h>
  18.  
  19.  
  20. static HINSTANCE hInstance;
  21. static char *sMallocClassName = "MallocClass",
  22.         *sRegMessage = MALLOC_MESSAGE;
  23. static UINT uRegMessage;
  24.  
  25.  
  26. LRESULT CALLBACK MallocProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  27.  
  28.  
  29. #pragma argsused
  30. int PASCAL WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine,
  31.             int nCmdShow)
  32.   {WNDCLASS wc;
  33.    MSG msg;
  34.    HWND hWnd;
  35.  
  36.    if (hPrev != NULL) return NULL;
  37.    hInstance = hInst;
  38.  
  39.    wc.style = CS_GLOBALCLASS;
  40.    wc.lpfnWndProc = MallocProc;
  41.    wc.cbClsExtra = 0;
  42.    wc.cbWndExtra = 0;
  43.    wc.hInstance = hInst;
  44.    wc.hIcon = NULL;
  45.    wc.hCursor = NULL;
  46.    wc.hbrBackground = NULL;
  47.    wc.lpszMenuName = NULL;
  48.    wc.lpszClassName = sMallocClassName;
  49.  
  50.    if (!RegisterClass (&wc)) return NULL;
  51.    uRegMessage = RegisterWindowMessage (sRegMessage);
  52.  
  53.    hWnd = CreateWindow (sMallocClassName, "", WS_OVERLAPPED,
  54.       0, 0, 0, 0, NULL, NULL, hInstance, NULL);
  55.    if (hWnd == NULL) return NULL;
  56.    ShowWindow (hWnd, SW_HIDE);
  57.  
  58.    while (GetMessage (&msg, NULL, 0, 0))
  59.      {TranslateMessage (&msg);
  60.       DispatchMessage (&msg);
  61.      } // end while
  62.  
  63.    return (int) msg.wParam;
  64.   } // end WinMain
  65.  
  66.  
  67. LRESULT CALLBACK MallocProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  68.   {HANDLE handle;
  69.    void far *lpPtr;
  70.    LPREALLOCSTRUCT lpRa;
  71.  
  72.    if (msg == uRegMessage)
  73.      {return SendMessage ((HWND) wParam, WM_COMMAND, HIWORD (lParam),
  74.      MAKELPARAM (hWnd, MAL_ACK));
  75.      } // endif
  76.  
  77.    switch (msg)
  78.      {case MAL_ALLOC:
  79.      handle = GlobalAlloc (GMEM_DDESHARE | GHND, (DWORD) lParam);
  80.      if (handle == NULL) return NULL;
  81.      lpPtr = GlobalLock (handle);
  82.      return (LRESULT) lpPtr;
  83.  
  84.       case MAL_REALLOC:
  85.      lpRa = (LPREALLOCSTRUCT) lParam;
  86.      handle = (HANDLE) LOWORD (GlobalHandle (SELECTOROF (lpRa->lpPtr)));
  87.      if (handle == NULL) return NULL;
  88.      if (GlobalUnlock (handle)) return NULL;
  89.      handle = GlobalReAlloc (handle, lpRa->dwSize, GMEM_ZEROINIT);
  90.      if (handle == NULL) return NULL;
  91.      lpPtr = GlobalLock (handle);
  92.      return (LRESULT) lpPtr;
  93.  
  94.       case MAL_FREE:
  95.      handle = (HANDLE) LOWORD (GlobalHandle (SELECTOROF ((void far *) lParam)));
  96.      if (handle == NULL) return 0;
  97.      GlobalUnlock (handle);
  98.      GlobalFree (handle);
  99.      return 0;
  100.  
  101.       case WM_CLOSE:
  102.      return 0;
  103.      } // end switch
  104.  
  105.    return DefWindowProc (hWnd, msg, wParam, lParam);
  106.   } // end MallocProc
  107.  
  108.  
  109.  
  110.